#include "jae.h"

/****************************************************//**
 * Project-Namespace
 ********************************************************/
namespace jae
{    QApplication* pApplication = NULL;

    /****************************************************//**
     * Initialize the engine and the logger.
     ********************************************************/
    QApplication* initEngine(int& argc, char ** argv)
    {
        Logger logger;
        logger.write("init JAE...\n");
        
        logger.createNewLogFile();

        logger.write("  init QT4...\n");
            if (pApplication == NULL)
            {
                pApplication = new QApplication(argc, argv);
            }
            if (!QGLFormat::hasOpenGL())
            {
                logger.error("ERROR", "JustAnotherEngine::JustAnotherEngine(int & argc, char ** argv)", "This system has no OpenGL support!");
                exit(EXIT_FAILURE);
            }
        logger.write("  init QT4 [OK]\n");
        
        logger.write("  analyze system...\n");
            getSystemInfos();
        logger.write("  analyze system [OK]\n");

        logger.writeln("init JAE [OK]");
        
        return pApplication;
    }

    /****************************************************//**
     * Shuts the engine down.
     ********************************************************/
    void closeEngine()
    {
        Logger logger;
        logger.write("shutdown JAE...\n");
            if (pApplication != NULL)
                    {
                        delete pApplication;
                    }
        logger.writeln("shutdown JAE [OK]");
    }    

    /****************************************************//**
     * This method fits the screenresolution to the given
     * values.
     * ATTENTION: This method is NOT OS-independent
     * TODO add MacOS Support 
     * @param aWidth new Screenwidth
     * @param aHeight new Screenheight
     * @param aDepth Colordepth in BitPerPixel (32bit, 16bit, ...)
     ********************************************************/
    void setScreenResolution(int aWidth, int aHeight, int aDepth)
    {
        Logger logger;
        logger.write("change screenresolution...\n");
    #if defined(THIS_IS_WINDOWS)
        
        DEVMODE screenMode;
        memset(&screenMode, 0 , sizeof(screenMode));
        screenMode.dmSize = sizeof(screenMode); 
        screenMode.dmPelsWidth = aWidth; 
        screenMode.dmPelsHeight = aHeight;
        screenMode.dmBitsPerPel = aDepth;
        screenMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
        
        if (ChangeDisplaySettings(&screenMode , CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
        {
            Logger logger;
            logger.error("ERROR", "void changeResolution(int aWidth, int aHeight, int aDepth)", "Change of Resolution failed!");
        }
    #elif defined(THIS_IS_UNIX_OR_LINUX)
        logger.error("NOT IMPLEMENTED YET", "void changeResolution(int aWidth, int aHeight, int aDepth)", "(not implemented for Unix/Linux)");
        logger.write(aWidth); logger.write("x"); logger.write(aHeight); logger.write("x"); logger.write(aDepth); logger.write(" \n");
    #else
        logger.error("NOT IMPLEMENTED YET", "void changeResolution(int aWidth, int aHeight, int aDepth)", "(not implemented for this operatingsystem)");
        logger.write(aWidth); logger.write("x"); logger.write(aHeight); logger.write("x"); logger.write(aDepth); logger.write(" \n");
    #endif
        logger.writeln("change screenresolution [OK]");
    }

    /****************************************************//**
     * This method tries to get some System/Compiler infos and
     * writes them into the logfile/console.
     * TODO add MacOS Support 
     * More infos here: http://pronix.linuxdelta.de/C/standard_C/c_programmierung_13.shtml
     ********************************************************/
    void getSystemInfos(void)
    {
        Logger logger;

    #ifdef __GNUC__
        logger.write("    This was compiled with GNU-C-Compiler!\n");
    #endif  //__GNUC__
        
    #ifdef _MSC_VER
        logger.write("    This was compiled with MS Visual C/C++!\n");
    #endif  //_MSC_VER

    #ifdef __WATCOMC__
        logger.write("    This was compiled with Watcom-C-Compiler!\n");
    #endif //__WATCOMC__

    #ifdef __NT__
        logger.write("    The operating system seems to be Windows NT!\n");
    #endif //__NT__
        
    #ifdef THIS_IS_WINDOWS
        logger.write("    The operating system seems to be Windows!\n");
    #endif //THIS_IS_WINDOWS

    #ifdef THIS_IS_UNIX_OR_LINUX
        logger.write("    The operating system seems to be a Unix/Linux!\n");
    #endif //THIS_IS_UNIX_OR_LINUX
        
    #ifdef __OS2__
        logger.write("    The operating system seems to be OS/2!\n");
    #endif //__OS2__
        
    #if (defined(__386__) || defined(__WINDOWS_386__))
        logger.write("    This program is compiled for 32bit processors!\n");
    #endif //__386__
    }

    
} //namespace jae
